package com.wxsm.wechat;

import com.wxsm.wechat.core.jpa.MyRepositoryFactoryBean;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.Base64Utils;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created with Yang Huan
 * Date: 2017/3/2
 * Time: 11:34
 */
@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class, JmxAutoConfiguration.class})
@ComponentScan({"com.wxsm.wechat.controller", "com.wxsm.wechat.core", "com.wxsm.wechat.service"})
@EnableTransactionManagement
@EnableAsync
@EnableJpaRepositories(repositoryFactoryBeanClass = MyRepositoryFactoryBean.class)
public class Application {

    private final static String KEYSTORE = "/u3+7QAAAAIAAAABAAAAAQAMd3d3LjV4NG0uY29tAAABWHDkglYAAAUBMIIE/TAOBgorBgEEASoCEQEBBQAEggTpw2vtD13o8LFIEn2lYDwXakYPY+OTHLAhy4/V98XvD7cxmPgdwwUHdbJq/9AzfrUMm0x2cT/uds8remPskE78tMY125ywfGSQe2HKg6BEIsbPluVTODwY48pn4EeMASMnvGne3BVftunW0aTJa+8iMNmegXovWKGxwxNdzjx4MgpHOWhx77dz1e2Js59vJq8juJ+QTkOH9iEdYeE4gwLtnzFJhOfYQzUflw4Hh+cyOh04Gt6toBmpScTe2R0o0+DvUs/er3xQQAV3CSPRReG+T2KlKKMyV0Tr6KH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qAviJG6tlbHcpHjohl1vziIgEh+/ti3EdOT5kOxhDpTPMs6FaIxaTPLB8/lsSNOWTj64ufr19x4Rx41q+eN9htHVZoKIZ2b5VWaMvF65W6QIDAQABo4IBdjCCAXIwEgYDVR0TAQH/BAgwBgEB/wIBADAvBgNVHR8EKDAmMCSgIqAghh5odHRwOi8vcy5zeW1jYi5jb20vcGNhMy1nNS5jcmwwDgYDVR0PAQH/BAQDAgEGMC4GCCsGAQUFBwEBBCIwIDAeBggrBgEFBQcwAYYSaHR0cDovL3Muc3ltY2QuY29tMGEGA1UdIARaMFgwVgYGZ4EMAQIBMEwwIwYIKwYBBQUHAgEWF2h0dHBzOi8vZC5zeW1jYi5jb20vY3BzMCUGCCsGAQUFBwICMBkaF2h0dHBzOi8vZC5zeW1jYi5jb20vcnBhMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjApBgNVHREEIjAgpB4wHDEaMBgGA1UEAxMRU3ltYW50ZWNQS0ktMi02MDEwHQYDVR0OBBYEFG1Yx38a5+E/LqaMlzVCu/TTOKw/MB8GA1UdIwQYMBaAFH/TZafC3ey78DAJ80M5+gKvMzEzMA0GCSqGSIb3DQEBCwUAA4IBAQAVKYYQW3hu7HBNxL3iDdHTo1z4MIEIvCNmQCmJug2sKBs9ZRCVXbxZpMjGz/e91/NG+5ipp9JzW3tZIwODNDtIR2jDNWWhL+3vbxxAKBssrDDuh8z1mDJMjcXAlezaLNlL+Ebyw6UB4ez+Cren1FNd4qXMp67UA9ZRVDNMqE88MBbD7tz5PveIS+fBK6sKiUMMaBzICyEHjFR3jSnFhctW3XUmyGwllGbOnDMYa62tOtmQqQwm8chGhTRgLWEDKjc+eWf9eF4ElDjWf3CU57OkCoaXIEzsAX5zc0Z5Pmlq3j+6LCoV5c+M7PpZT+4KAQyhTjDLaBF8d2IyQioKapgVRzvEHzvIdbBFIMkFO7ydsG+A1hQ=";

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainer() throws IOException {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        tomcat.addAdditionalTomcatConnectors(createSslConnector());
        return tomcat;
    }

    //开启https
    private Connector createSslConnector() throws IOException {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        File keystoreFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "keystore");
        FileCopyUtils.copy(Base64Utils.decodeFromString(KEYSTORE), keystoreFile);
        connector.setScheme("https");
        connector.setSecure(true);
        connector.setPort(443);
        protocol.setSSLEnabled(true);
        protocol.setKeystoreFile(keystoreFile.getAbsolutePath());
        protocol.setKeystorePass("yanghuan198887");
        protocol.setKeystoreType("JKS");
        return connector;
    }
}
